package com.example.hs.Controller;


import com.example.hs.DTO.OrderDTO;
import com.example.hs.Pojo.Order;
import com.example.hs.Pojo.Recycler;
import com.example.hs.Pojo.User;
import com.example.hs.Service.OrderService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    // 创建预约订单
    @PostMapping("/create")
    public ResponseEntity<Order> createOrder(@RequestBody OrderDTO orderDTO, HttpSession session) {
        return ResponseEntity.ok(orderService.createOrder(orderDTO,session));
    }
    // 获取用户预约订单
    @GetMapping("/user")
    public ResponseEntity<List<Order>> getOrdersByUserId(HttpSession session) {
        User currentUser = (User)session.getAttribute("user");
        int userId = currentUser.getUserId();
        return ResponseEntity.ok(orderService.getOrdersByUserId(userId));
    }
    // 根据订单id
    @GetMapping("/{orderId}")
    public ResponseEntity<Order> getOrderById(@PathVariable int orderId) {
        Order order = orderService.getOrderById(orderId);
        return ResponseEntity.ok(order);
    }
    @GetMapping("/recycler")
    public ResponseEntity<List<Order>> getOrdersByRecyclerId(HttpSession session) {
        Recycler recycler = (Recycler) session.getAttribute("recycler");
        int recyclerId = recycler.getRecyclerId();
        return ResponseEntity.ok(orderService.getOrdersByRecyclerId(recyclerId));
    }
    /**/
    /*用户取消预约订单*/
    @PostMapping("/delete/{orderId}")
    public ResponseEntity<String> deleteOrderById(@PathVariable int orderId) {
        // 检查订单是否存在
        return ResponseEntity.ok(orderService.deleteOrderById(orderId));
    }
    /*把状态从待接单0变更成进行中1*/
    @PutMapping("/status1/{orderId}")
    public ResponseEntity<String> updateOrderStatus(@PathVariable int orderId) {
        return ResponseEntity.ok(orderService.updateOrderStatusById(orderId));
    }
    /*把订单状态从进行中1变更成已完成2*/
    @PutMapping("/status2/{orderId}")
    public ResponseEntity<String> updateOrderStatus2(@PathVariable int orderId) {
        return ResponseEntity.ok(orderService.updateOrderStatusById2(orderId));
    }
    /*拒接：把订单状态从未接单0变成拒接3*/
    @PutMapping("/status3/{orderId}")
    public ResponseEntity<String> updateOrderStatus3(@PathVariable int orderId) {
        return ResponseEntity.ok(orderService.updateOrderStatusById3(orderId));
    }
    /*根据订单id修改订单的重量和金额*/
    @PutMapping("/update/{orderId}")
    public ResponseEntity<Order> updateOrder(@PathVariable int orderId, @RequestBody Order updatedOrder) {
        Order order = orderService.updateOrder(orderId, updatedOrder);
        return ResponseEntity.ok(order);
    }

}